package com.leo.erp.web.role;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leo.erp.dto.RoleExecution;
import com.leo.erp.dto.UserBusinessExecution;
import com.leo.erp.entity.Role;
import com.leo.erp.entity.User;
import com.leo.erp.enums.RoleStateEnum;
import com.leo.erp.enums.UserBusinessStateEnum;
import com.leo.erp.service.RoleService;
import com.leo.erp.service.UserBusinessService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/role")
public class RoleManagementController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserBusinessService userBusinessService;
	
	@RequestMapping(value = "/findUserRole")
	@ResponseBody
	public JSONArray findUserRole(HttpServletRequest request) {
		//获取前台传过来的值
		String type = HttpServletRequestUtil.getString(request, "UBType");
		String keyId = HttpServletRequestUtil.getString(request, "UBKeyId");
		JSONArray arr = new JSONArray();
		RoleExecution re = roleService.findUserRole();
		if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
			for (Role role : re.getRoleList()) {
                JSONObject item = new JSONObject();
                item.put("id", role.getRoleId());
                item.put("text", role.getName());
                //勾选判断1
                Boolean flag = false;
                try {
                    UserBusinessExecution ue = userBusinessService.checkIsUserBusinessExist(type, keyId, "[" + role.getRoleId() + "]");
                    if(ue.getState() == UserBusinessStateEnum.SUCCESS.getState()) {
                    	flag = true;
                    }
                } catch (GlobalRuntimeException e) {
                    e.printStackTrace();
                }
                if (flag == true) {
                    item.put("checked", true);
                }
                arr.add(item);
            }
		}		
		return arr;
	}
	
	@RequestMapping(value = "/updateRole")
	@ResponseBody
	public Map<String, Object> updateRole(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int roleId = HttpServletRequestUtil.getInt(request, "roleId");
		String name = HttpServletRequestUtil.getString(request, "info");//取到的值为{"name":"xxx"}
		name = name.substring(9, name.length()-2);//xxx
		
		RoleExecution re = new RoleExecution();
		re = roleService.updateRole(roleId, name);
		if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/checkIsNameExist")
	@ResponseBody
	public Map<String, Object> checkIsNameExist(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String name = HttpServletRequestUtil.getString(request, "name");
		
		RoleExecution re = new RoleExecution();
		try {
			//调用方法判断名字是否已经存在
			re = roleService.checkIsNameExist(name);
			if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
				//存在
				modelMap.put("success", true);
				modelMap.put("flag", true);
			}else {
				//不存在
				modelMap.put("success", false);
				modelMap.put("msg", "可以创建");
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}	
		return modelMap;
	}
	
	@RequestMapping(value = "/addRole")
	@ResponseBody
	public Map<String, Object> addRole(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String name = HttpServletRequestUtil.getString(request, "info");//取到的值为{"name":"xxx"}
		name = name.substring(9, name.length()-2);//xxx
		
		//session获取用户信息
		User user = (User) request.getSession().getAttribute("user");
		
		Role role = new Role();
		if(name != null && name != "") {
			role.setName(name);
		}
		role.setTenantId(user.getTenantId());
		role.setDeleteFlag("0");
		
		RoleExecution re = roleService.addRole(role, request);
		if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "插入数据失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String name = HttpServletRequestUtil.getString(request, "name");
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		RoleExecution re = roleService.getRoleList(name, currentPage, pageSize);
		if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
			modelMap.put("pages",re.getPages());//总页数
			modelMap.put("total", re.getTotal());//总数
			Map<String, Object> map = new HashMap<>();
			map.put("rows", re.getRolePageList());
			map.put("total", re.getTotal());
			
			modelMap.put("roleList", map);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteRoleById")
	@ResponseBody
	public Map<String, Object> deleteRoleById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前端传过来的值
		String ids = HttpServletRequestUtil.getString(request, "ids");
		
		RoleExecution re = roleService.deleteRoleById(ids);
		if(re.getState() == RoleStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除角色信息失败");
		}
		return modelMap;
	}
}
